//完成图片上传
const multer = require("multer");
const path = require("path");
const fs = require("fs");
let uploadFile = {};

//获得数据库中模板为Upload的表
const Upload = require("../models/Upload");
// 确保../public/uploads目录存在
const uploadDir = path.join(__dirname, "..", "public", "uploads");
if (!fs.existsSync(uploadDir)) {
  fs.mkdirSync(uploadDir, { recursive: true });
}

// 图片上传配置
const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, "./public/uploads");
  },
  filename: (req, file, cb) => {
    const ext = path.extname(file.originalname);
    cb(null, `${Date.now()}${ext}`);
  },
});

// 图片上传中间件
const upload = multer({ storage });

module.exports = upload;
